
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// This file was initially generated by gen_to_start.go (add link), as a start
// of the Traffic Ops golang data model

package api

import (
	"encoding/json"
	_ "github.com/apache/incubator-trafficcontrol/traffic_ops/experimental/server/output_format" // needed for swagger
	"github.com/jmoiron/sqlx"
	null "gopkg.in/guregu/null.v3"
	"log"
)

type DeliveryservicesRegexes struct {
	Deliveryservice string                       `db:"deliveryservice" json:"deliveryservice"`
	RegexId         int64                        `db:"regex_id" json:"regexId"`
	SetNumber       null.Int                     `db:"set_number" json:"setNumber"`
	Links           DeliveryservicesRegexesLinks `json:"_links" db:-`
}

type DeliveryservicesRegexesLinks struct {
	Self string `db:"self" json:"_self"`
}

// @Title getDeliveryservicesRegexesById
// @Description retrieves the deliveryservices_regexes information for a certain id
// @Accept  application/json
// @Param   id              path    int     false        "The row id"
// @Success 200 {array}    DeliveryservicesRegexes
// @Resource /api/2.0
// @Router /api/2.0/deliveryservices_regexes/{id} [get]
func getDeliveryservicesRegex(deliveryservice string, regexId int64, db *sqlx.DB) (interface{}, error) {
	ret := []DeliveryservicesRegexes{}
	arg := DeliveryservicesRegexes{}
	arg.Deliveryservice = deliveryservice
	arg.RegexId = regexId
	queryStr := "select *, concat('" + API_PATH + "deliveryservices_regexes', '/deliveryservice/', deliveryservice, '/regex_id/', regex_id) as self"
	queryStr += " from deliveryservices_regexes WHERE deliveryservice=:deliveryservice AND regex_id=:regex_id"
	nstmt, err := db.PrepareNamed(queryStr)
	err = nstmt.Select(&ret, arg)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	nstmt.Close()
	return ret, nil
}

// @Title getDeliveryservicesRegexess
// @Description retrieves the deliveryservices_regexes
// @Accept  application/json
// @Success 200 {array}    DeliveryservicesRegexes
// @Resource /api/2.0
// @Router /api/2.0/deliveryservices_regexes [get]
func getDeliveryservicesRegexes(db *sqlx.DB) (interface{}, error) {
	ret := []DeliveryservicesRegexes{}
	queryStr := "select *, concat('" + API_PATH + "deliveryservices_regexes', '/deliveryservice/', deliveryservice, '/regex_id/', regex_id) as self"
	queryStr += " from deliveryservices_regexes"
	err := db.Select(&ret, queryStr)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	return ret, nil
}

// @Title postDeliveryservicesRegexes
// @Description enter a new deliveryservices_regexes
// @Accept  application/json
// @Param                 Body body     DeliveryservicesRegexes   true "DeliveryservicesRegexes object that should be added to the table"
// @Success 200 {object}    output_format.ApiWrapper
// @Resource /api/2.0
// @Router /api/2.0/deliveryservices_regexes [post]
func postDeliveryservicesRegex(payload []byte, db *sqlx.DB) (interface{}, error) {
	var v DeliveryservicesRegexes
	err := json.Unmarshal(payload, &v)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	sqlString := "INSERT INTO deliveryservices_regexes("
	sqlString += "deliveryservice"
	sqlString += ",regex_id"
	sqlString += ",set_number"
	sqlString += ") VALUES ("
	sqlString += ":deliveryservice"
	sqlString += ",:regex_id"
	sqlString += ",:set_number"
	sqlString += ")"
	result, err := db.NamedExec(sqlString, v)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	return result, err
}

// @Title putDeliveryservicesRegexes
// @Description modify an existing deliveryservices_regexesentry
// @Accept  application/json
// @Param   id              path    int     true        "The row id"
// @Param                 Body body     DeliveryservicesRegexes   true "DeliveryservicesRegexes object that should be added to the table"
// @Success 200 {object}    output_format.ApiWrapper
// @Resource /api/2.0
// @Router /api/2.0/deliveryservices_regexes/{id}  [put]
func putDeliveryservicesRegex(deliveryservice string, regexId int64, payload []byte, db *sqlx.DB) (interface{}, error) {
	var arg DeliveryservicesRegexes
	err := json.Unmarshal(payload, &arg)
	arg.Deliveryservice = deliveryservice
	arg.RegexId = regexId
	if err != nil {
		log.Println(err)
		return nil, err
	}
	sqlString := "UPDATE deliveryservices_regexes SET "
	sqlString += "deliveryservice = :deliveryservice"
	sqlString += ",regex_id = :regex_id"
	sqlString += ",set_number = :set_number"
	sqlString += " WHERE deliveryservice=:deliveryservice AND regex_id=:regex_id"
	result, err := db.NamedExec(sqlString, arg)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	return result, err
}

// @Title delDeliveryservicesRegexesById
// @Description deletes deliveryservices_regexes information for a certain id
// @Accept  application/json
// @Param   id              path    int     false        "The row id"
// @Success 200 {array}    DeliveryservicesRegexes
// @Resource /api/2.0
// @Router /api/2.0/deliveryservices_regexes/{id} [delete]
func delDeliveryservicesRegex(deliveryservice string, regexId int64, db *sqlx.DB) (interface{}, error) {
	arg := DeliveryservicesRegexes{}
	arg.Deliveryservice = deliveryservice
	arg.RegexId = regexId
	result, err := db.NamedExec("DELETE FROM deliveryservices_regexes WHERE deliveryservice=:deliveryservice AND regex_id=:regex_id", arg)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	return result, err
}
